Playing a Movie
The easiest way to play a movie is to use a movie controller component. See the previous section for more information about using movie controller components. If you want to create your own control for playing movies, you should observe the following guidelines:
Once you have loaded a movie, you can play the movie. Your application must perform the following tasks:
- Your application should allow the user to manipulate movies in the same way
that your application allows the user to work with static graphics--the user should be able to select, resize, cut, copy, and paste movies.- Your application should save the current position of each movie in a document.
- Your application should not automatically play the movies in a document when the user opens the document.
- You should keep your movie controls simple and close to the movie.
- You should be consistent in the way that you allow the user to play a movie. Do not use single-clicking and double-clicking for the same thing. In general, use a single click to select a movie and use a double click to play it.
- When printing, your application should print each movie's current frame. You may choose to allow the user to select the frame for each movie, perhaps by means of a special menu item. Be sure not to print any of the user controls.
When you play a movie, the Movie Toolbox processes the movie's data in the context of the movie's time coordinate system. If the movie contains video data, the Movie Toolbox displays the resulting image in the display window you specify. If the movie contains audio data, the Movie Toolbox plays that sound track at the volume you set.
- Create a window for the movie to play in.
- Position the movie in the window.
- Start the movie.
- Play the movie until it is done.
- Dispose of the movie when it is done playing.
You must call the
MoviesTask
function (described on page 2-110) repeatedly until the movie is done playing. Each time you call theMoviesTask
function, the Movie Toolbox processes the movie you are playing, updates the display as appropriate, and uses the Sound Manager to play the movie's sound. You can use theIsMovieDone
function (described on page 2-112) to determine when the movie is finished playing.The code in Listing 2-4 shows the steps your application must follow in order to play a movie. This program retrieves a movie, sizes the window properly, plays the movie forward, and exits. This program uses the
GetMovie
function, shown in Listing 2-2 on page 2-27 to retrieve a movie from a movie file. The movie controller component supplied by Apple also plays a movie. For more information, see the chapter "Movie Controller Components" in Inside Macintosh: QuickTime Components.
#include <Types.h>#include <Traps.h>#include <Menus.h>#include <Fonts.h>#include <Packages.h>#include <GestaltEqu.h>#include "Movies.h"#include "ImageCompression.h"/* #include "QuickTimeComponents.h" */ #define doTheRightThing 5000 void main (void) { WindowPtr aWindow; Rect windowRect; Rect movieBox; Movie aMovie; Boolean done = false; OSErr err; EventRecord theEvent; WindowPtr whichWindow; short part; InitGraf (&qd.thePort); InitFonts (); InitWindows (); InitMenus (); TEInit (); InitDialogs (nil); err = EnterMovies (); if (err) return; SetRect (&windowRect, 100, 100, 200, 200); aWindow = NewCWindow (nil, &windowRect, "\pMovie", false, noGrowDocProc, (WindowPtr)-1, true, 0); SetPort (aWindow); aMovie = GetMovie (); if (aMovie == nil) return; GetMovieBox (aMovie, &movieBox); OffsetRect (&movieBox, -movieBox.left, -movieBox.top); SetMovieBox (aMovie, &movieBox); SizeWindow (aWindow, movieBox.right, movieBox.bottom, true); ShowWindow (aWindow); SetMovieGWorld (aMovie, (CGrafPtr)aWindow, nil); StartMovie (aMovie); while ( !IsMovieDone(aMovie) && !done ) { if (WaitNextEvent (everyEvent, &theEvent, 0, nil)) { switch ( theEvent.what ) { case updateEvt: whichWindow = (WindowPtr)theEvent.message; if (whichWindow == aWindow) { BeginUpdate (whichWindow); UpdateMovie(aMovie); SetPort (whichWindow); EraseRect (&whichWindow->portRect); EndUpdate (whichWindow); } break; case mouseDown: part = FindWindow (theEvent.where, &whichWindow); if (whichWindow == aWindow) { switch (part) { case inGoAway: done = TrackGoAway (whichWindow, theEvent.where); break; case inDrag: DragWindow (whichWindow, theEvent.where, &qd.screenBits.bounds); break; } } break; } } MoviesTask (aMovie, DoTheRightThing); } DisposeMovie (aMovie); DisposeWindow (aWindow); }